package com.ruoyi.fun.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.fun.mapper.EcUserCouponNewMapper;
import com.ruoyi.fun.domain.EcUserCouponNew;

import com.ruoyi.fun.service.IEcUserCouponNewService;


/**
 * 优惠券列表Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-08-26
 */
@Service
public class EcUserCouponNewServiceImpl implements IEcUserCouponNewService 
{
    @Autowired
    private EcUserCouponNewMapper ecUserCouponNewMapper;
    


    /**
     * 查询优惠券列表
     * 
     * @param couponId 优惠券列表主键
     * @return 优惠券列表
     */
    @Override
    public EcUserCouponNew selectEcUserCouponNewByCouponId(Long couponId)
    {
        return ecUserCouponNewMapper.selectEcUserCouponNewByCouponId(couponId);
    }

    /**
     * 查询优惠券列表列表
     * 
     * @param ecUserCouponNew 优惠券列表
     * @return 优惠券列表
     */
    @Override
    public List<EcUserCouponNew> selectEcUserCouponNewList(EcUserCouponNew ecUserCouponNew)
    {
        return ecUserCouponNewMapper.selectEcUserCouponNewList(ecUserCouponNew);
    }

    /**
     * 新增优惠券列表
     * 
     * @param ecUserCouponNew 优惠券列表
     * @return 结果
     */
    @Override
    public int insertEcUserCouponNew(EcUserCouponNew ecUserCouponNew)
    {
        // 如果couponId为空，生成一个唯一的ID
        if (ecUserCouponNew.getCouponId() == null) {
            // 使用时间戳 + 随机数生成唯一ID
            long timestamp = System.currentTimeMillis();
            int random = (int)(Math.random() * 1000);
            Long couponId = Long.parseLong(String.valueOf(timestamp).substring(8) + String.format("%03d", random));
            ecUserCouponNew.setCouponId(couponId);
        }
        
        // 如果orgId为空，设置为当前用户所属的部门ID
        if (ecUserCouponNew.getOrgId() == null) {
            ecUserCouponNew.setOrgId(SecurityUtils.getDeptId());
        }
        
        ecUserCouponNew.setCreateTime(DateUtils.getNowDate());
        return ecUserCouponNewMapper.insertEcUserCouponNew(ecUserCouponNew);
    }

    /**
     * 修改优惠券列表
     * 
     * @param ecUserCouponNew 优惠券列表
     * @return 结果
     */
    @Override
    public int updateEcUserCouponNew(EcUserCouponNew ecUserCouponNew)
    {
        ecUserCouponNew.setUpdateTime(DateUtils.getNowDate());
        return ecUserCouponNewMapper.updateEcUserCouponNew(ecUserCouponNew);
    }

    /**
     * 批量删除优惠券列表
     * 
     * @param couponIds 需要删除的优惠券列表主键
     * @return 结果
     */
    @Override
    public int deleteEcUserCouponNewByCouponIds(Long[] couponIds)
    {
        return ecUserCouponNewMapper.deleteEcUserCouponNewByCouponIds(couponIds);
    }

    /**
     * 删除优惠券列表信息
     * 
     * @param couponId 优惠券列表主键
     * @return 结果
     */
    @Override
    public int deleteEcUserCouponNewByCouponId(Long couponId)
    {
        return ecUserCouponNewMapper.deleteEcUserCouponNewByCouponId(couponId);
    }

    /**
     * 根据创建用户ID和组织ID查询用户优惠券列表
     * 
     * @param createUserId 创建用户ID
     * @param orgId 组织ID
     * @return 用户优惠券集合
     */
    @Override
    public List<EcUserCouponNew> selectEcUserCouponNewListByCreateUserIdAndOrgId(Long createUserId, Long orgId)
    {
        return ecUserCouponNewMapper.selectEcUserCouponNewListByCreateUserIdAndOrgId(createUserId, orgId);
    }
}
